package com.woniuxy.service.impl;

import com.woniuxy.dao.UserMapper;
import com.woniuxy.pojo.Role;
import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> findAllUsers() {
        return userMapper.selectAllUsers();
    }

    @Override
    public User login(String telephone, String password) {
        return userMapper.selectUserByTelAndPwd(telephone,password);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addUser(User user) {
        boolean flag=false;
        this.userMapper.insertSysUser(user);
        //获取该用户的所有角色
        List<Role> roles=user.getRoles();
        if(roles!=null&&roles.size()>0){
            for(Role role:roles){
                this.userMapper.insertSysUserRole(user.getId(),role.getId());
            }
        }
        flag=true;
        return flag;
    }
}
